//
//  STDSSolidColorLayer.hpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/20.
//

#ifndef STDSSolidColorLayer_hpp
#define STDSSolidColorLayer_hpp

#include <stdio.h>
#include "STDBaseLayer.hpp"
#include <Effects/STDSSolidColorEffect.h>
/// 纯色层
class STDSSolidColorLayer: public STDBaseLayer {
public:
    STDSSolidColorLayer() {
        setType(STDLayerTypeSolidColor);
    }
    
    void draw() override {
        shared_ptr<STDSRenderTarget> inputFbo = make_shared<STDSRenderTarget>(mSizeSRC.x, mSizeSRC.y);
        inputFbo->bindSelf();
        auto effect = getEffect();
        effect->setColor(vec4(mColor.x, mColor.y, mColor.z, 1.0));
        effect->mSizeSRC = mSizeSRC;
        effect->draw();
        inputFbo->restore();
        
        for (auto effect: effects) {
            if (effect->isVisible() == false) {
                continue;
            }
            auto fbo = make_shared<STDSRenderTarget>(mSizeSRC.x, mSizeSRC.y);
            effect->mSizeSRC = mSizeSRC;
            fbo->bindSelf();
            effect->setTexture(inputFbo->getTexture());
            effect->draw();
            fbo->restore();
            inputFbo = fbo;
        }
        drawTexture(inputFbo->getTexture());
    }
    
    void setColor(float &r, float &g, float &b) {
        mColor = vec3(r, g, b);
    }
private:
    shared_ptr<STDSSolidColorEffect> getEffect() {
        if (mSolidEffect == nullptr) {
            mSolidEffect = make_shared<STDSSolidColorEffect>();
            mSolidEffect->setContext(mContext);
        }
        return mSolidEffect;
    }
private:
    shared_ptr<STDSSolidColorEffect> mSolidEffect{nullptr};
    vec3 mColor{0.0, 0.0, 0.0};
};
#endif /* STDSSolidColorLayer_hpp */
